#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from dae.socketclient import SocketClient
from dae.postmessage import PostMessage
from PyQt5.Qt import QApplication
import socket
from threading import Lock,  Thread
from PyQt5.QtCore import QObject,  pyqtSlot,  pyqtSignal
from dae.userinfo import UserInfo

class ManipulateData(QObject):
    
    onstart = pyqtSignal()
    onstop = pyqtSignal()
    
    onheartsignal = pyqtSignal(int)
    onheartBPM = pyqtSignal(int)
    onheartIBI = pyqtSignal(int)
    onheartTemp = pyqtSignal(int)
    
    onhealthrespond = pyqtSignal(str)
    ongethealthrespond = pyqtSignal(list)
    onloginsuccess = pyqtSignal()
    
    def __init__(self,  parent):
        super(ManipulateData,  self).__init__(parent)
        
        self.userinfo = UserInfo()
        self.userinfo.loginState= False
        self.bpm = "0"
        self.ibi = "0"
        self.temp = "0"
        
        self.socketclient = SocketClient(self)
        self.socketclient.onmessage.connect(self.distributor)
        self.socketclient .connect("127.0.0.1",  4999)
        
        self.postHealth = PostMessage()
        self.getHealth = PostMessage()
        self.postLogin = PostMessage()
        
        self.postHealth.onmessage.connect(self.postHealthRespond)
        self.getHealth.onmessage.connect(self.getHealthRespond)
        self.postLogin.onmessage.connect(self.postLoginRespond)
    
    #Slots
    
    @pyqtSlot(str)
    def distributor(self,  message):
        print (message)
        message = message.split("#")
        #print (message)
        message = message[1:]
        
           #来自GateWay的信息
        for submsg in message:
            
            if len(submsg) <= 2:
                continue;
            #print (submsg,  "---len", len(submsg) )
            if submsg[0] == 'S':                 #heartSignal
                self.heartSignal(submsg)
            elif submsg[0] == 'B':               #BPM
                self.heartBPM(submsg)
            elif submsg[0] == 'Q':               #IBI
                self.heartIBI(submsg)
            elif submsg[0] == 'T':
                self.heartTemp(submsg)          #temperature
            else:
                pass
    
    def postHealthMsg(self):
        content = "userid=" + self.userinfo.id + '&' \
                        + "hrateInterval=" + self.ibi + '&' \
                        + "hrateavg=" + self.bpm+ '&' \
                        + "temperature=" + self.temp
        self.postHealth.post("http://120.24.172.121:8080/syhealth/user/user_subHInfo",  content)
    
 
    def getHealthMsg(self):
        content = "userid="  + self.userinfo.id
        self.getHealth.post("http://120.24.172.121:8080/syhealth/user/user_listForDesktop?", content )        
        
    def postLoginMsg(self,  account,  passward):
        content = "username="  + "kkkk" + "&" \
                        + "password=" + "0000"
        self.postLogin.post("http://120.24.172.121:8080/syhealth/user/user_login?", content )
        

        
    #Signals
    
    def heartSignal(self,  signal):
        signal = int(signal[1:])
        #print (signal)
        self.onheartsignal.emit(signal)    
        
    def heartBPM(self,  bpm):
        self.bpm = bpm[1:]
        bpm = int(bpm[1:])
        self.onheartBPM.emit(bpm)
        
    def heartIBI(self,  ibi):
        self.ibi = ibi[1:]
        ibi = int(ibi[1:])
        self.onheartIBI.emit(ibi)

    def heartTemp(self,  temp):
        self.temp = temp[1:]
        temp = int(temp[1:])
        self.onheartTemp.emit(temp)
        
    def postHealthRespond(self,  respond):
        print (respond)
        self.getHealthMsg()
        #self.onhealthrespond.emit(respond)
        
    def getHealthRespond(self,  respond):
        print (respond)
        respond = respond['rows']
        self.ongethealthrespond.emit(respond)

        
    def postLoginRespond(self,  respond):
        
        #print (respond)
        if respond['result'] == 'success' and self.userinfo.loginState == False:
            self.userinfo.loginState = True
            self.userinfo.id = respond['data']
            self.onloginsuccess.emit()
            self.getHealthMsg()
        else:
            print (respond['data'])
        
    
        
